package com.yloveg.controller;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yloveg.base.BaseController;
import com.yloveg.common.FreemarkUtil.State;
import com.yloveg.common.FreemarkUtil.YesOrNo;
import com.yloveg.common.PageUtil;
import com.yloveg.entity.ProductContent;
import com.yloveg.entity.ShopInfo;
import com.yloveg.service.ProductContentService;
import com.yloveg.service.ShopInfoService;


@Controller
@RequestMapping({"shops"})  
public class ShopsController extends BaseController {  

    @Autowired
    private ProductContentService productContentService;
    
    @Autowired
    private ShopInfoService shopInfoService;
 
    
    /**
     * 店铺信息
     * @return
     */
    @RequestMapping("/shopInfo")
    public String shopInfo(ModelMap map){
    	ShopInfo info = new ShopInfo();
    	map.put("info", info);
    	info.setUserId(this.getSessionUser().getId());
    	info = shopInfoService.queryOneShopInfo(info);
    	if(info!=null){
    		map.put("info", info);	
    	}
    	return "landing/shops/shopInfo";
    }
    /**
     * 保存或更新店铺信息
	* @param info
	* @return
	*/
	@RequestMapping("/saveUpShopInfo")
	@ResponseBody
	public Map<String, Object> saveUpShopInfo (ShopInfo info) {
		Map<String, Object> map = new HashedMap<String, Object>();
		int result = 0;
		if(info.getId()==0) {
			info.setShopAtive(YesOrNo.YES.getValue());
			info.setShopStatus(State.IN_AUDIT.getValue());
			info.setUserId(this.getSessionUser().getId());
			result = shopInfoService.insertShopInfo(info);
		}else {
			result = shopInfoService.updateShopInfo(info);
		}
		if(result>0){
			this.setSessionShop(info);
			map.put("code", "1");
			map.put("msg", "操作成功！");
		}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
		}
		return map;
	}
	
    /**
     * 模板列表
     * @return
     */
    @RequestMapping("/templateList")
    public String templateList(ModelMap map){
    	map.put("shopInfo", this.getSessionShop());
        return "landing/shops/templateList";
    }
    
    @RequestMapping("/saveStyle")
    @ResponseBody
    public Map<String, Object> saveStyle(String styleId){
    	 Map<String, Object> map  = new HashMap<String, Object>();
    	 ShopInfo info = this.getSessionShop();
    	 if(info!=null) {
    		 info.setShopStyle(styleId);
    		 shopInfoService.updateShopInfo(info);
    		 map.put("code", "1");
    		 map.put("msg", "选择成功！");
    		 this.setSessionShop(info);
    	 }else {
    		 map.put("code", "2");
    		 map.put("msg", "轻完善店铺信息！");    		 
    	 }
    	 return map; 
    } 
    
    /**
     * 产品列表
     * @return
     */
    @RequestMapping("/productList")
    public String productList(ModelMap map){
        return "landing/shops/productList";
    }

    @RequestMapping("/queryProductContentPage")
    @ResponseBody
    public Map<String, Object> queryProductContentPage(int page, int limit,ProductContent product){
    	if(this.getSessionShop().getId()!=null) {
            product.setShopId(this.getSessionShop().getId());
            Map<String, Object> map = PageUtil.dateToLayTable(productContentService.queryProductContentPage(page,limit,product));
            return map;
    	}else {
    		product.setShopId(-1l);
            Map<String, Object> map = PageUtil.dateToLayTable(productContentService.queryProductContentPage(page,limit,product));
            return map;
    	}
        
    }
    /**
     * 新增产品
     * @return
     */
    @RequestMapping("/addProduct")
    public String addProduct(Long id,ModelMap map){
    	if(id==null) {
    		map.put("product", new ProductContent());
    	}else {
    		ProductContent product = productContentService.selectByPrimaryKey(id);
    		map.put("product", product);
    	}
    	return "landing/shops/addProduct";
        
    }
    /**
     * 保存产品信息
	* @param info
	* @return
	*/
	@RequestMapping("/saveUpProduct")
	@ResponseBody
	public Map<String, Object> saveUpProduct (ProductContent product) {
		Map<String, Object> map = new HashedMap<String, Object>();
		int result = 0;
		if(this.getSessionShop().getId()!=null) {
			if(product.getId()==0) {
				product.setPublisherId(this.getSessionUser().getId());
				product.setPublisherName(this.getSessionUser().getUsername());
				product.setShopId(this.getSessionShop().getId());
				result = productContentService.insertProduct(product);
			}else {
				result = productContentService.updateProduct(product);
			}
			if(result>0){
				map.put("code", "1");
				map.put("msg", "操作成功！");
			}else {
				map.put("code", "0");
				map.put("msg", "系统错误请联系管理员！");
			}
		}else {
			map.put("code", "2");
			map.put("msg", "您还没有店铺信息，请完善！");
		}

		return map;
	}
    /**
     * 删除产品信息
	* @param info
	* @return
	*/
	@RequestMapping("/deleteProduct")
	@ResponseBody
	public Map<String, Object> deleteProduct (ProductContent product) {
		Map<String, Object> map = new HashedMap<String, Object>();
		int result = 0;
		if(product.getId()!=null) {
			product.setState(0);
			result = productContentService.updateProduct(product);
		}
		if(result>0){
			map.put("code", "1");
			map.put("msg", "操作成功！");
		}else {
			map.put("code", "0");
			map.put("msg", "系统错误请联系管理员！");
		}
		return map;
	}
	
	
}
